package com.richard.base.ui

import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.richard.base.module.BaseBean
import com.richard.base.view.RefreshHeader

/**
 ***************************************
 * 项目名称:base
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/1/3     2:06 PM
 * 用途: fragment 基类
 ***************************************

 */

abstract class BaseVMListFragment<VM : ViewModel, T : BaseBean> : BaseVMFragment<VM>() {

    val startPageNo = 0
    private var pageSize = 10
    private var pageNo: Int = startPageNo

    abstract fun getRecycleView(): RecyclerView
    abstract fun getAdapter(): BaseQuickAdapter<T, *>

    abstract fun getRefreshLayout() : TwinklingRefreshLayout

    protected abstract fun reqList(pageNo: Int, pageSize: Int)

    protected open fun listEmpty() {

    }

    private var isRevertLoad = false

    private var preNo: Int = startPageNo - 1
    private var nextNo: Int = startPageNo

    override fun initView() {
        setRecycleAdapter(getAdapter(), getRecycleView())

        setRefreshLayout(getRefreshLayout())
    }

    private fun setRecycleAdapter(adapter: BaseQuickAdapter<*, *>, recycleView: RecyclerView) {
        adapter.loadMoreModule.isEnableLoadMore = true
        adapter.loadMoreModule.setOnLoadMoreListener {
            setRevertLoad(false)
            reqList(pageNo, pageSize)
        }

        adapter.upFetchModule.setOnUpFetchListener {
            setRevertLoad(true)
            reqList(pageNo, pageSize)
        }

        recycleView.adapter = adapter
    }

    private fun setRefreshLayout(trl_content : TwinklingRefreshLayout) {
        context?.let {
            // 下拉刷新组件
            val headerView = RefreshHeader(it)
            trl_content.setHeaderView(headerView)
            trl_content.setOverScrollRefreshShow(false)
            trl_content.setEnableOverScroll(false)
            trl_content.setEnableLoadmore(false)
            trl_content.setOnRefreshListener(object : RefreshListenerAdapter() {
                override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                    resetPageNo()
                    initData()
                }
            })
        }
    }

    protected open fun setReqListSuccess(objects: MutableList<T>) {
        if (pageNo == startPageNo) {
            if (objects == null || objects.isEmpty()) {
                listEmpty()
            }
            getAdapter().setNewInstance(objects)
        }
        if (objects.isNotEmpty()) {
            if (isRevertLoad()) {
                preNo--
                pageNo = preNo
                getAdapter().addData(0, objects)
            } else {
                nextNo++
                pageNo = nextNo
                getAdapter().addData(objects)
            }
        }
        getAdapter().loadMoreModule.loadMoreComplete()
        getAdapter().loadMoreModule.isEnableLoadMore = objects.size >= pageSize
        getAdapter().upFetchModule.isUpFetching = false
        getAdapter().upFetchModule.isUpFetchEnable = preNo >= startPageNo
    }

    protected open fun resetPageNo() {
        pageNo = startPageNo
        preNo = startPageNo - 1
        nextNo = startPageNo
    }

    protected open fun resetPageNo(num: Int) {
        pageNo = num
        preNo = num - 1
        nextNo = num
    }

    open fun getPageNo(): Int {
        return pageNo
    }

    open fun getPageSize(): Int {
        return pageSize
    }

    open fun resetPageSize(pageSize: Int) {
        this.pageSize = pageSize
    }

    open fun isRevertLoad(): Boolean {
        return isRevertLoad
    }

    open fun setRevertLoad(revertLoad: Boolean) {
        isRevertLoad = revertLoad
        getAdapter().upFetchModule.isUpFetching = true
        pageNo = if (revertLoad) {
            preNo
        } else {
            nextNo
        }
    }
}